import { _decorator, Collider2D, Component, Contact2DType, IPhysics2DContact, Node, Vec3 } from 'cc';
import { enemyContral } from './enemyContral';
import { TabEnum } from '../enum';
const { ccclass, property } = _decorator;

@ccclass('bulletContral')
export class bulletContral extends Component {
    @property
    speed:number = 800;
    start() {
        const collider = this.getComponent(Collider2D)
        if(collider){
            collider.on(Contact2DType.BEGIN_CONTACT,this.onBeginContact,this)
        }
    }
    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        console.log('碰撞开始：子弹',otherCollider.tag);
        //如果碰到敌人就销毁自己让敌人死亡
        if(otherCollider.tag == TabEnum.ENEMY){
            otherCollider.node.getComponent(enemyContral).die()
            this.node.destroy()
            // otherCollider.node.getComponent('enemyContral').die()
        }
    }

    update(deltaTime: number) {
        let newPosition = new Vec3(
            this.node.position.x,
            this.node.position.y + this.speed * deltaTime,
            this.node.position.z
        )
        this.node.setPosition(newPosition)
        //超出屏幕销毁
        if(this.node.position.y>820){
            this.node.destroy()
        }
    }
}


